/**
 * 
 */
package com.ybxx.core.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ybxx.system.model.User;


/**
 * <p>Title:</p>
 * <p>Description: </p>
 * <P>Copyright: Copyright (c) 2010-2015</P>
 * <P>Company: XA.Co.LTD</P>
 * @author:姚强
 * @version 1.0
 */
public class CheckLoginFilter implements Filter {

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest arg0, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request=(HttpServletRequest)arg0;
		HttpServletResponse response=(HttpServletResponse)resp;
		String context=request.getContextPath();
		String url=request.getRequestURI();
		if(!url.equals(context+"/") && !url.equals(context+"/index.jsp") && url.indexOf("login")<0){
			User user=(User)request.getSession(true).getAttribute("currentUser");
			if(user==null){
				response.sendRedirect("../index.jsp?flag=0");
			}else{
				chain.doFilter(arg0, resp);
			}
		}else{
			chain.doFilter(arg0, resp);
		}
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}

